var oldfw;
var app = new Vue({
    el: '#ppage',
    data() {
        return {
            BASE_PATH: BASE_PATH,
            itemId: "",
            flids: "",
            imgs: [],
            zd:1,
            fw:0
        };
    },
    mounted() {
        this.itemId = getQueryVariable("itemId");
        var flid = getQueryVariable("flid");
        var flname=getQueryVariable("flname");
        var fromrelease=localStorage.getItem("from");
        if(!fromrelease){
            localStorage.setItem("flids",flid);
            localStorage.setItem("flnames",decodeURI(flname));
        }
        _getImgFromLocalStorage(this);
        _getFlFromLocalStorage(this);
        oldfw=this.fw;
        bindbutton(this);
        localStorage.removeItem("from");
    },
    methods: {
        showSites: function (alldatas) {
            this.sites = alldatas;
        },
        goupload: function () {
            window.location.href = "release_upload.html"
        },
        selectfl:function(){
            var flids=$("#flids").val();
            window.location.href = "my_flc_select.html?flids="+flids;
        },
        selectfw:function(){
            document.getElementById('fwList').className = "pay-list animated bounceIn";
        },
        closeFw:function() {
            document.getElementById('fwList').className = "pay-list hidden-class";
            this.fw=oldfw;
        },
        deleteImg:function(imgPath){
            var that=this;
            layer.open({
                content: "删除这张图片吗？"
                , btn: ['确定', '取消']
                , yes: function (index) {
                    that.imgs.remove(imgPath);
                    localStorage.setItem("uploadImgs",JSON.stringify(that.imgs));
                    layer.close(index);
                }
            });
        }
    },
    updated() {

    }
});
//绑定按钮
function bindbutton(that) {
    // 发福利和置顶按钮
    var buttons = document.querySelector('.top-button');
    buttons.onclick = function () {
        if (this.parentNode.parentNode.classList.contains('state-open')) {
            this.parentNode.parentNode.className = "common-list";
            that.zd=0;
        } else {
            this.parentNode.parentNode.className = "common-list state-open";
            that.zd=1;
        }
    }
    $("#fbBtn").click(function(){
        var obj={};
        obj.id=$("#itemId").val();
        obj.title=$("#title").val();
        obj.content=$("#content").val();
        obj.cPhone=$("#cPhone").val();
        obj.shen=localStorage.getItem("shen");
        obj.si=localStorage.getItem("si");
        obj.dq=localStorage.getItem("dq");
        obj.jd=localStorage.getItem("jd");
        obj.userId=userModel.id;
        obj.location=localStorage.getItem("location");
        obj.zd=that.zd;
        obj.zdTime=$("#zdTime").val();
        obj.flIds=$("#flids").val();
        obj.qwPrice=$("#qwPrice").val();
        obj.keywords=$("#keywords").val();
        obj.imagePaths="";
        if(localStorage.getItem("uploadImgs")!=null){
            obj.imagePaths=JSON.parse(localStorage.getItem("uploadImgs")).join(",");
        }
        obj.fw=that.fw;
        if(obj.title==""||!obj.title){
            mymsg("标题不能为空")
            return false;
        }
        if(obj.content==""||!obj.content){
            mymsg("内容不能为空")
            return false;
        }
        if(obj.cPhone==""||!obj.cPhone){
            mymsg("电话号码不能为空")
            return false;
        }
        if(obj.zdTime!=""&&!isDate(obj.zdTime)){
            mymsg("日期输入的格式不对")
            return false;
        }
        if(obj.flids==""){
            mymsg("请选择信息的分类")
            return false;
        }
        if(!isNumber(obj.qwPrice)){
            mymsg("对方支付金额只能是数字")
            return false;
        }
        if(obj.keywords==""||!obj.keywords){
            mymsg("信息关键词不能为空")
            return false;
        }
        //保存信息
        $.ajax({
            type: "post",
            url: BASE_PATH + "/app/item/edit",
            dataType: "json", //返回数据形式为json
            data: obj,
            success: function (result) {
                if (result.code == 0) {
                    //上传成功清除本地存贮
                    localStorage.removeItem('flids');
                    localStorage.removeItem('flnames');
                    localStorage.removeItem('uploadImgs');
                    //清空表单
                    clearForm(that)
                    layer.open({
                        content: "信息发布成功"
                        , skin: 'msg'
                        , time: 5
                    });
                } else {
                    layer.open({
                        content: result.msg
                        , skin: 'msg'
                        , time: 5
                    });
                }
            }
        });
    })
    $("#fw0").click(function(){
        that.fw=0;
    });
    $("#fw1").click(function(){
        that.fw=1;
    });
}
//得到分类数据从本地存贮
function _getFlFromLocalStorage(that){
    var flids = localStorage.getItem("flids");
    var flnames = localStorage.getItem("flnames");
    if (flids) {
        $("#flids").val(flids)
    }
    if(flnames){
        $("#flnames").val(flnames)
    }
}
//得到图片数据从本地存贮
function _getImgFromLocalStorage(that) {
    var imgs = JSON.parse(localStorage.getItem("uploadImgs"));
    if (imgs) {
        that.imgs = imgs;
    }
}
//得到信息数据
function _getItemData(that) {
    //保存信息
    $.ajax({
        type: "post",
        url: BASE_PATH + "/app/fb/getflbyfbandmy",
        dataType: "json", //返回数据形式为json
        data: { id: userModel.id },
        success: function (result) {
            if (result.code == 0) {
                layer.open({
                    content: "信息发布成功"
                    , skin: 'msg'
                    , time: 5
                });
            } else {
                layer.open({
                    content: result.msg
                    , skin: 'msg'
                    , time: 5
                });
            }
        }
    });
}
/**
 * 确定窗口
 */
 function doFw() {
    document.getElementById('fwList').className = "pay-list hidden-class";
}

function clearForm(that){
    $("#itemId").val("");
    $("#title").val("");
    $("#content").val("");
    $("#cPhone").val("");
    $("#zdTime").val("");
    $("#flids").val("");
    $("#flnames").val("");
    $("#qwPrice").val("");
    $("#keywords").val("");
    that.imgs=[];
    localStorage.removeItem("uploadImgs")
}
